<HTML>
<HEAD>
<TITLE>[Chapter 16] 16.5 StringSelection</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:57:03 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch16_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 16<br>Data Transfer</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch16_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-16-SECT-5">16.5 StringSelection</A></h2>

<P CLASS=para>
<tt CLASS=literal>StringSelection</tt> is a convenience 
class that can be used for copy and paste operations on Unicode text strings 
(<tt CLASS=literal>String</tt>). It implements both 
the <tt CLASS=literal>ClipboardOwner</tt> and <tt CLASS=literal>Transferable</tt> 
interfaces, so it can be used both as the contents of the clipboard and 
as its owner. For example, if <tt CLASS=literal>s</tt> 
is a <tt CLASS=literal>StringSelection</tt>, you can 
call <tt CLASS=literal>Clipboard.setContents(s,s)</tt>. 
<tt CLASS=literal>StringSelection</tt> supports both 
<tt CLASS=literal>stringFlavor</tt> and <tt CLASS=literal>plainTextFlavor</tt> 
and doesn't do anything when it loses clipboard ownership. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-16-SECT-5.1">StringSelection Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public StringSelection(String data) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The constructor creates an instance of <tt CLASS=literal>StringSelection</tt> 
containing <tt CLASS=literal>data</tt>. You can use 
this object to place the data on a clipboard. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public DataFlavor[] getTransferDataFlavors() <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getTransferDataFlavors()</tt> 
method returns a two-element <tt CLASS=literal>DataFlavor</tt> 
array consisting of <tt CLASS=literal>DataFlavor.stringFlavor</tt> 
and <tt CLASS=literal>DataFlavor.plainTextFlavor</tt>. 
This means that you can paste a <tt CLASS=literal>StringSelection</tt> 
as either a Java <tt CLASS=literal>String</tt> or 
as plain text (i.e., the MIME type <tt CLASS=literal>plain/text</tt>). 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean isDataFlavorSupported(DataFlavor flavor) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>isDataFlavorSupported()</tt> 
method is returns <tt CLASS=literal>true</tt> if <tt CLASS=literal>flavor</tt> 
is either <tt CLASS=literal>DataFlavor.stringFlavor</tt> 
or <tt CLASS=literal>DataFlavor.plainTextFlavor</tt>; 
it returns <tt CLASS=literal>false</tt> for any other 
flavor. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getTransferData()</tt> method 
returns an object from which you can get the data on the clipboard; the 
object's type is determined by the <tt CLASS=literal>flavor</tt> 
parameter. This method returns a <tt CLASS=literal>String</tt> 
containing the data on the clipboard if <tt CLASS=literal>flavor</tt> 
is <tt CLASS=literal>DataFlavor.stringFlavor</tt>; 
it returns a <tt CLASS=literal>StringBufferInputStream</tt> 
from which you can read the data on the clipboard if you ask for <tt CLASS=literal>DataFlavor.plainTextFlavor</tt>. 
Otherwise, <tt CLASS=literal>getTransferData()</tt> 
throws an <tt CLASS=literal>UnsupportedFlavorException</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void lostOwnership(Clipboard clipboard, Transferable contents) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>lostOwnership()</tt> method 
of <tt CLASS=literal>StringSelection</tt> is an empty 
stub; it does nothing when you lose ownership. If you want to know when 
you've lost ownership of string data placed on the clipboard, write 
a subclass of <tt CLASS=literal>StringSelection</tt> 
and override this method. </DL>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch16_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch16_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Clipboard</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>UnsupportedFlavorException</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
